package design.pattern.Lu04TemplatePattern;

/**
 * @program JavaBase
 * @description: 模版模式，父类依赖子类，即 修改子类，影响了父类的执行结果
 *  父类可以提供钩子方法，让子类去控制，然后，即可控制父类的模版方法
 * @author: zhanglu
 * @create: 2020-03-27 16:37:00
 */
public class Lu04TemplatePattern {

    public static void main(String[] args) {
        Car01 car = new Car01();
        car.setAlarm(false);
        car.run();
    }

}

abstract class Car{

    abstract protected void getName();

    abstract protected void getColor();

    abstract protected void getVoice();

    protected final void run(){
        this.getName();
        this.getColor();
        if(isAlarm()){
            this.getVoice();
        }
    }

    //钩子方法，默认喇叭是会响的
    protected boolean isAlarm(){
        return true;
    }

//    protected boolean flag = true;

}

class Car01 extends Car{

    private boolean flag;

    public void setAlarm(boolean alarm){
        this.flag = alarm;
    }

    @Override
    protected void getName() {
        System.out.println("汽车01");
    }

    @Override
    protected void getColor() {
        System.out.println("白色");
    }

    @Override
    protected void getVoice() {
        System.out.println("滴滴滴");
    }

    @Override
    protected boolean isAlarm() {
        return this.flag;
    }
}


